<template>
    <div class="edit-popup">
        <popup
            ref="popupRef"
            :title="popupTitle"
            :async="true"
            width="750px"
            @confirm="handleSubmit"
            @close="handleClose"
        >
            <el-form
                ref="formRef"
                :model="formData"
                :rules="formRules"
                label-width="100px"
                class="mt-4"
            >
                <el-form-item label="所属公司" prop="companyId">
                    <el-select v-model="formData.companyId" class="w-[280px]">
                        <el-option label="拼哒-成都-携程-tocc" value="1" />
                        <el-option label="拼哒-厦门-交通部" value="2" />
                    </el-select>
                </el-form-item>
                <el-form-item label="订单金额" prop="amount">
                    <el-input-number v-model="formData.amount" :precision="2" :step="0.1" class="w-[280px]" />
                </el-form-item>
                <el-form-item label="司机姓名" prop="driverName">
                    <el-input v-model="formData.driverName" class="w-[280px]" />
                </el-form-item>
                <el-form-item label="车牌号" prop="carNumber">
                    <el-input v-model="formData.carNumber" class="w-[280px]" />
                </el-form-item>
                <el-form-item label="驾驶证号" prop="driverLicense">
                    <el-input v-model="formData.driverLicense" class="w-[280px]" />
                </el-form-item>
                <el-form-item label="出发地址" prop="startAddress">
                    <el-input v-model="formData.startAddress" class="w-[500px]" />
                </el-form-item>
                <el-form-item label="目的地址" prop="endAddress">
                    <el-input v-model="formData.endAddress" class="w-[500px]" />
                </el-form-item>
                <el-form-item label="订单时间" required>
                    <el-row :gutter="20">
                        <el-col :span="8">
                            <el-form-item prop="createTime">
                                <el-date-picker
                                    v-model="formData.createTime"
                                    type="datetime"
                                    placeholder="下单时间"
                                />
                            </el-form-item>
                        </el-col>
                        <el-col :span="8">
                            <el-form-item prop="startTime">
                                <el-date-picker
                                    v-model="formData.startTime"
                                    type="datetime"
                                    placeholder="出发时间"
                                />
                            </el-form-item>
                        </el-col>
                        <el-col :span="8">
                            <el-form-item prop="arriveTime">
                                <el-date-picker
                                    v-model="formData.arriveTime"
                                    type="datetime"
                                    placeholder="到达时间"
                                />
                            </el-form-item>
                        </el-col>
                    </el-row>
                </el-form-item>
            </el-form>
        </popup>
    </div>
</template>

<script lang="ts" setup>
import type { FormInstance } from 'element-plus'
import Popup from '@/components/popup/index.vue'
import feedback from '@/utils/feedback'

const emit = defineEmits(['success', 'close'])
const formRef = shallowRef<FormInstance>()
const popupRef = shallowRef<InstanceType<typeof Popup>>()
const mode = ref('add')
const popupTitle = computed(() => {
    return mode.value === 'add' ? '新增补单' : '编辑补单'
})

const formData = reactive({
    companyId: '',
    amount: 0,
    driverName: '',
    carNumber: '',
    driverLicense: '',
    startAddress: '',
    endAddress: '',
    createTime: '',
    startTime: '',
    arriveTime: ''
})

const formRules = {
    companyId: [{ required: true, message: '请选择所属公司' }],
    amount: [{ required: true, message: '请输入订单金额' }],
    driverName: [{ required: true, message: '请输入司机姓名' }],
    carNumber: [{ required: true, message: '请输入车牌号' }],
    driverLicense: [{ required: true, message: '请输入驾驶证号' }],
    startAddress: [{ required: true, message: '请输入出发地址' }],
    endAddress: [{ required: true, message: '请输入目的地址' }],
    createTime: [{ required: true, message: '请选择下单时间' }],
    startTime: [{ required: true, message: '请选择出发时间' }],
    arriveTime: [{ required: true, message: '请选择到达时间' }]
}

const handleSubmit = async () => {
    await formRef.value?.validate()
    const data = { ...formData }
    // 这里需要调用实际的API
    feedback.msgSuccess(mode.value === 'add' ? '新增成功' : '编辑成功')
    emit('success')
    handleClose()
}

const handleClose = () => {
    emit('close')
}

const open = (type = 'add') => {
    mode.value = type
    popupRef.value?.open()
}

const getDetail = async (data: Record<string, any>) => {
    Object.assign(formData, data)
}

defineExpose({
    open,
    getDetail
})
</script>